import java.util.Arrays;
import java.util.Scanner;
import java.util.SortedMap;

public class Test {


    private boolean isCharacter(char ch) {
        if((ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9')) {
            return true;
        }
        return false;
    }

    public boolean isPalindrome(String s) {
        String str = s.toUpperCase();
        int i = 0;
        int j = str.length()-1;
        while(i < j) {
            if(!isCharacter(str.charAt(i))) {
                i++;
            }
            if(isCharacter(str.charAt(j))) {
                j--;
            }
            if(str.charAt(i) == str.charAt(j)) {
                i++;
                j--;
            }else {
                return false;
            }
        }
        return true;
    }

    public static void main13(String[] args) {

        Scanner scan = new Scanner(System.in);
        while(scan.hasNextLine()) {
            String str = scan.nextLine();
            int index = str.lastIndexOf(" ");
            String str2 = str.substring(index+1);
            System.out.println(str2.length());
        }

//        Scanner in = new Scanner(System.in);
//        // 注意 hasNext 和 hasNextLine 的区别
//        // while (in.hasNextInt()) { // 注意 while 处理多个 case
//        //     int a = in.nextInt();
//        //     int b = in.nextInt();
//        //     System.out.println(a + b);
//        // }
//        while (in.hasNextLine()) {
//            String str = in.nextLine();
//            str.trim();
//            String[] strings = str.split(" ");
//            for(int i = 0; i < strings.length; i++) {
//
//            }
//        }
    }

    public static void main12(String[] args) {
        String str = "hello";// final修饰，不能改变指向；private修饰，不能改变内容
        str += "world";
        System.out.println(str); // 此时str是全新的对象
    }

    public static void main11(String[] args) {
        char[] char1 = {'a', 'b', 'c'};
        String str1 = new String(char1);
        String str2 = "abc";
        System.out.println(str1 == str2); // false

        char[] char2 = {'d', 'e', 'f'};
        String str3 = new String(char2);
        str3.intern(); // 常量池没有str3，手动入池
        String str4 = "def";
        System.out.println(str3 == str4); // true
    }

    public static void main10(String[] args) {
        String s1 = "hello";
        String ret = "he" + "llo";
        System.out.println(s1 == ret); // true,"he"和"llo"都是常量，编译的时候默认是"hello"

        String s2 = "he";
        String s3 = "llo";
        String s4 = s1 + s2;
        System.out.println(s1 == s4); // false,s2+s3都是变量，在编译的时候不知道是什么
    }

    public static void main9(String[] args) {
        String str1 = "   abc   defg   ";
        String str2 = str1.trim(); // 去掉字符串左右空格
        System.out.println(str1); //   abc   defg
        System.out.println(str2); //abc   defg
    }

    public static void main8(String[] args) {
        String str1 = "abcdefg";
        String str2 = str1.substring(2,5); // 截取字符串
        System.out.println(str2);
    }

    public static void main7(String[] args) {
        String str1 = "name=zhangsan&age=18";
        String[] strings = str1.split("&");
        for (String s : strings) {
            String[] s1 = s.split("=");
            for (String x : s1) {
                System.out.println(x);
            }
        }
    }

    public static void main6(String[] args) {
        String str1 = "abcdef\\gha";
        String[] strings = str1.split("\\\\");
        for (String s : strings) {
            System.out.println(s);
        }
        // abcdef
        // gha
    }

    public static void main5(String[] args) {
        String str1 = "abc def&gha";
        String[] strings = str1.split(" |&");
        for (String s : strings) {
            System.out.println(s);
        }
    }

    public static void main4(String[] args) {
        String str1 = "abc def gha";
        String[] strings = str1.split(" ");
        for (String s : strings) {
            System.out.println(s);
        }

        String str2 = "192.168.1.1";
        String[] string = str2.split("\\.", 2);
        for (String s : string) {
            System.out.println(s);
        }
    }

    public static void main3(String[] args) {
        String str1 = "abcdabababa";

        String str2 = str1.replace('a', 'm');
        System.out.println(str2); // mbcdmbmbmbm

        String str3 = str1.replace("ab", "mt");
        System.out.println(str3); // mtcdmtmtmta

        String str4 = str1.replace("ab", "mt");
        System.out.println(str3); // mtcdmtmtmta

        String str5 = str1.replaceAll("ab", "mt");
        System.out.println(str3); // mtcdmtmtmta

        String str6 = str1.replaceFirst("ab", "mt");
        System.out.println(str6); // mtcdabababa

    }

    public static void main2(String[] args) {
        String s = "hello";
        char[] ch = s.toCharArray();
        System.out.println(Arrays.toString(ch)); // [h, e, l, l, o]

        String s1 = String.format("%d-%d-%d", 2019, 3,23);
        System.out.println(s1); // 2019-3-23
    }

    public static void main1(String[] args) {
        String s1 = new String("hello");
        String s2 = new String("hello");
        // 只要进行 new ，就会开辟新的内容
        String s3 = "hello";
        String s4 = "hello";

        System.out.println(s1 == s2); // false
        System.out.println(s3 == s4); // true
        System.out.println(s1.equals(s2)); // true
        System.out.println(s1.compareTo(s2)); // 0
    }

    public static void main0(String[] args) {
        // 使用常量串构造
        String str1 = "frost cold";
        System.out.println(str1);

        // 直接newString对象
        String str2 = new String("frost cold");
        System.out.println(str2);

        // 使用字符数组进行构造
        char[] ch = {'f', 'r', 'o', 's', 't', ' ', 'c', 'o', 'l', 'd'};
        String str3 = new String(ch);
        System.out.println(str3);
    }

    public static void main02(String[] args) {
        String s1 = "frost cold";
        String s2 = "";
        String s3 = " ";
        System.out.println(s1.isEmpty());
        System.out.println(s2.isEmpty());
        System.out.println(s3.isEmpty());
    }

    public static void main01(String[] args) {
        // s1和s2引用的是不同对象 s1和s3引用的是同一对象
        String s1 = new String("hello");
        String s2 = new String("world");
        String s3 = s1;
        System.out.println(s1.length()); // 获取字符串长度---输出5
        System.out.println(s1.isEmpty()); // 如果字符串长度为0，返回true，否则返回false
    }
}
